//
//  ViewController.m
//  UI22_多线程
//
//  Created by 李劲成 on 16/4/8.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+DownloadImage.h"
@interface ViewController ()

@property (nonatomic, retain)UIImage *imageOfHead;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadImage];
//    [self.imageView asynDownloadImageWithURL:@"http://img5.cache.netease.com/3g/2015/10/30/20151030124521c1f7e.jpg" completionHadnle:^(NSData *data, UIImage *image) {
//        
//    }];
//    [self chapterOfThread];
//    [self chapterOfNSOPerationQueue];
    [self chapterOfGCD];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 代码块
- (void)snippet {
    NSLog(@"snippet's thread: %@", [NSThread currentThread]);
    
    NSUInteger sum = 0;
    
    // 循环10亿次.
    for (NSUInteger i = 0; i < 100000000; i++) {
        
        sum += i;
        
    }
    NSLog(@"sum :%ld", sum);
}

#pragma mark - 知识点: GCD

#pragma mark ** GCD 创建

- (void)chapterOfGCD {

#if 0
    // 创建串行队列
   dispatch_queue_t queue = dispatch_queue_create("com.lanou3g", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        [self snippet];
    });
    
    dispatch_async(queue, ^{
        [self snippet];
    });
#endif

    // 创建并发队列.
    dispatch_queue_t queue = dispatch_queue_create("com.lanou3g", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        [self snippet];
    });
    dispatch_async(queue, ^{
        [self snippet];
    });
}

#pragma mark ** GCD 应用场景 - 单例

+ (instancetype)sharedViewController {

    static ViewController *vc = nil;
    // 只运行一次.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        vc = [[ViewController alloc]init];
    });
    return vc;
}

#pragma mark ** GCD 应用场景 - 图片异步加载
- (void)loadImage {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // 在子线程运行下载图片代码.
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img5.cache.netease.com/3g/2015/10/30/20151030124521c1f7e.jpg"]];
        // 返回主线程, UI赋值
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // core API
            self.imageOfHead = [UIImage imageWithData:data];
            
        });
        
    });
}

#pragma mark - 知识点: NSThread
- (void)chapterOfThread {
    
    // 创建一个子线程
    NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(snippet) object:nil];
    thread1.name = @"循环";
    // 手动开始线程.

    [thread1 start];
    
    // 子线程自动开启
    [NSThread detachNewThreadSelector:@selector(snippet) toTarget:self withObject:nil];
}

#pragma mark - 知识点: NSOperationQueue
- (void)chapterOfNSOPerationQueue {
    
    // 操作队列(NSOperationQueue)中是操作对象.
    NSInvocationOperation *invoc1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(snippet) object:nil];
    NSInvocationOperation *invoc2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(snippet) object:nil];
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        [self snippet];
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//    queue.maxConcurrentOperationCount = 2;
    [queue addOperation:invoc1];
    [queue addOperation:invoc2];
    [queue addOperation:op3];
    
}

- (void)chapterOfObject {
    [self performSelectorInBackground:@selector(snippet) withObject:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
